import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

/**
 * 
 * @author Enzo
 *
 * Modularizes the to-do list part of the main screen
 *
 *Functions:
 *	Paint onto this screen
 *	Refresh screen
 *	
 */

public class ToDoListGUIComponent {
	
	//Class variables
	private JPanel toDoListPanel;
	
	
	/**
	 * Deafault Constructor 
	 */
	public ToDoListGUIComponent(){
		//Initialize the toDoListFrame
		toDoListPanel = new JPanel();
		toDoListPanel.setBorder(BorderFactory.createRaisedBevelBorder());

		
		/*
		 * After adding all the current items, add in a button that allows
		 * users to create more toDoItems
		 */
		addCreateNewItem_Button();
		
	}
	
	public void refreshToDoComponent(){
		//This wipes out the entire pane and refills it with the todolist
		toDoListPanel.removeAll();
		
		for(int i =0; i<DataManagement.toDoList.size(); i ++){
			addToDoItemRow(DataManagement.toDoList.get(i));
		}
		toDoListPanel.revalidate();
		toDoListPanel.repaint();
		addCreateNewItem_Button();//don't forget to add on the create new item button
	}
	
	public void addToDoItemRow(ToDoItem item){	
		//Each item will have a separate row
		JPanel itemPanel = new JPanel();
		itemPanel.setBorder(BorderFactory.createLineBorder(null));
		itemPanel.setLayout(new BoxLayout(toDoListPanel, 0));
		itemPanel.setMinimumSize(new Dimension(100, 30));
		
		Dimension textBoxMinSize = new Dimension(100, 30);
		Dimension textBoxMaxSize = new Dimension(300, 30);
		
		JTextField nameTextBox = new JTextField(item.getToDoString());
		nameTextBox.setMinimumSize(textBoxMinSize);
		nameTextBox.setMaximumSize(textBoxMaxSize);
//		nameTextBox.setPreferredSize(new Dimension(100, 30));

		JTextField priorityTextBox = new JTextField(item.getPriority()+"");
		priorityTextBox.setMinimumSize(textBoxMinSize);
		priorityTextBox.setMaximumSize(textBoxMaxSize);
//		priorityTextBox.setPreferredSize(new Dimension(100, 30));
		
		JTextField dateTextBox = new JTextField(item.getDate().toString());
		dateTextBox.setMinimumSize(textBoxMinSize);
		dateTextBox.setMaximumSize(textBoxMaxSize);
//		dateTextBox.setPreferredSize(new Dimension(100, 30));
		
		itemPanel.setLayout(new FlowLayout());
		itemPanel.add(nameTextBox);
		itemPanel.add(priorityTextBox);
		itemPanel.add(dateTextBox);
		
		addRemoveItem_Button(itemPanel, item);
		
		//Finally, add this panel to the frame holding all of the toDoItems
		toDoListPanel.add(itemPanel);
	}
	public void removeToDoItemGUI(int itemID){
		
	}
	
	public void paintOnFrame(JFrame frameToPaintOn){
		frameToPaintOn.add(toDoListPanel);	
	}
	
	
	
	
	//Building GUI buttons------------------------------------------------------------------
	public void addCreateNewItem_Button(){
		JButton makeToDoItemButton = new JButton("+");
		toDoListPanel.add(makeToDoItemButton);
		makeToDoItemButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				createNewToDoItem();
			}
		});
	}
	
	public void addRemoveItem_Button(JPanel panel, final ToDoItem item){
		JButton makeToDoItemButton = new JButton("-");
		panel.add(makeToDoItemButton);
		makeToDoItemButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				removeToDoItem(item);
			}
		});
	}
	
	
	//Action Listeners-----------------------------------------------------------------------------
	public void createNewToDoItem(){
		//Create a popup dialog to ask user to for information about TO DO item
		
		//1)Create a custom panel to put into the pop up (showConfirmDialog)
		JPanel userOptionPanel = new JPanel();
		Date toDoDate = new Date();
		
		HintTextField enterDescriptionTextBox = new HintTextField("Enter your description here");
		HintTextField enterDateTextBox = new HintTextField(toDoDate.toString());
		HintTextField enterPriorityTextBox = new HintTextField("Enter Priority here");
		

		userOptionPanel.add(enterDescriptionTextBox);
		userOptionPanel.add(enterDateTextBox);
		userOptionPanel.add(enterPriorityTextBox);
		
		
//		int result = JOptionPane.showConfirmDialog(userOptionPanel, "Create a ToDo Item", "This is OptionPane Title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null);
		int result = JOptionPane.showConfirmDialog(toDoListPanel, userOptionPanel, "This is OptionPane Title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null);
		
		//Check the user's choice
		if(result == JOptionPane.OK_OPTION){
			//Create a new ToDo item
			ToDoItem newItem = new ToDoItem(toDoDate, enterDescriptionTextBox.getText(), Integer.parseInt(enterPriorityTextBox.getText()));
			DataManagement.createNewToDoItem(newItem);
			refreshToDoComponent();
		}else{}
	}
	
	/**
	 * This removes the ToDoItem from the GUI
	 * @param item 
	 */
	public void removeToDoItem(ToDoItem item){
		DataManagement.toDoList.remove(item);
		refreshToDoComponent();
	}
	
	
	
	
	
	
	
	
	//Helper Classes------------------------------------------------------------------------
	
	/**
	 * Provides a customized textfield that has a hint integrated into it
	 * @author Enzo
	 *
	 */
	class HintTextField extends JTextField implements FocusListener {

	    private final String hint;

	    public HintTextField(final String hint) {
	        super(hint);
	        super.setForeground(Color.LIGHT_GRAY);
	        this.hint = hint;
	        super.addFocusListener(this);
	    }
	    @Override
	    public void focusGained(FocusEvent e) {
	        if(this.getText().isEmpty()) {
	        	super.setForeground(Color.DARK_GRAY);
	            super.setText("");
	        }
	    }
	    @Override
	    public void focusLost(FocusEvent e) {
	        if(this.getText().isEmpty()) {
	        	super.setForeground(Color.LIGHT_GRAY);
	            super.setText(hint);
	        }
	    }
	    @Override
	    public String getText() {
	        String typed = super.getText();
	        return typed.equals(hint) ? "" : typed;
	    }
	}
	
	

}
